package socketVersion.Backend.Rooms;

import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;

import socketVersion.GenericServer;
import socketVersion.Reply;

public class RoomServer extends GenericServer{
	private RoomServerImplementation roomServer;
	
	public RoomServer(int port) throws IOException {
		super(port);
		roomServer = new RoomServerImplementation();
	}

	@Override
	protected Reply handleMethod(String methodName, LinkedList<Object> args) {
		LinkedList<Object> returnValues;
		Reply reply = null;
		//In the event that parameter cast fails
		try{
			if("addRooms".equals(methodName)&&(args.size()==4)){
				int id = ((Integer)args.get(0)).intValue();
				String location = (String)args.get(1);
				int numRooms = ((Integer)args.get(2)).intValue();
				int price = ((Integer)args.get(3)).intValue();
				boolean result;
				synchronized(roomServer){
					result = roomServer.addRooms(id, location, numRooms, price);
				}
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			if("deleteRooms".equals(methodName)&&(args.size()==2)){
				int id = ((Integer)args.get(0)).intValue();
				String location = (String)args.get(1);
				boolean result;
				synchronized(roomServer){
					result = roomServer.deleteRooms(id, location);
				}
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			if("queryRooms".equals(methodName)&&(args.size()==2)){
				int id = ((Integer)args.get(0)).intValue();
				String location = (String)args.get(1);
				int result;
				synchronized(roomServer){
					result = roomServer.queryRooms(id, location);
				}
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			if(("queryRoomsPrice".equals(methodName))&&(args.size()==2)){
				int id = ((Integer)args.get(0)).intValue();
				String location = (String)args.get(1);
				int result;
				synchronized(roomServer){
					result = roomServer.queryRoomsPrice(id, location);
				}
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			if("reserveRoom".equals(methodName)&&(args.size()==3)){
				int id = ((Integer)args.get(0)).intValue();
				int customer = ((Integer)args.get(1)).intValue();
				String location = (String)args.get(2);
				boolean result;
				synchronized(roomServer){
					result = roomServer.reserveRoom(id, customer, location);
				}
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			if("deleteReservation".equals(methodName)&&(args.size()==2)){
				int id = ((Integer)args.get(0)).intValue();
				int customer = ((Integer)args.get(1)).intValue();
				boolean result;
				synchronized(roomServer){
					result = roomServer.deleteReservation(id, customer);
				}
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			if("calculateBill".equals(methodName)&&(args.size()==2)){
				int id = ((Integer)args.get(0)).intValue();
				int customer = ((Integer)args.get(1)).intValue();
				int result;
				synchronized(roomServer){
					result = roomServer.calculateBill(id, customer);
				}
				returnValues = new LinkedList<Object>();
				returnValues.add(result);
				reply = new Reply(Reply.STATUS_SUCCESS,returnValues);
			}
			
			//None worked
			if(reply!=null){
				return reply;
			}
			else{
				return new Reply(Reply.STATUS_METHOD_NOT_FOUND,new LinkedList<Object>());
			}
			
		} catch(Exception e){
			e.printStackTrace();
			return new Reply(Reply.STATUS_BAD_REQUEST,new LinkedList<Object>());
		}
	}
	
	public static void main(String[] args){
		if(args.length!=1){
			System.out.println("Specify port to run the server on as first argument");
			System.exit(0);
		}
		int port = 0;
		try{
			port = Integer.parseInt(args[0]);
		} catch(NumberFormatException e){
			System.out.println("Specify port to run the server on as first argument");
			System.exit(0);
		}
		
		
		
		RoomServer server;
		try {
			server = new RoomServer(port);
			server.start();
		} catch (IOException e) {
			System.err.println("Server was unable to start");
			e.printStackTrace();
		}
	}

	
	
}
